package com.xoa.service.crm;

import com.xoa.dao.crm.CrmContractMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.crm.CrmContract;
import com.xoa.model.crm.Product;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.glassfish.jersey.spi.Contract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liuqian on 2019/5/30.
 */
@Service
public class CrmContractService {

    @Autowired
    private CrmContractMapper crmContractMapper;
    @Resource
    private UsersMapper usersMapper;

    public void addCrmContract(HttpServletRequest request, CrmContract crmContract) {
        //合同编号
        String number = "";

        //截取时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMM");
        String strDate = dateFormat.format(date);
        //查询最后一条数据
        CrmContract crmContract1 = crmContractMapper.selectContractNo();
        Integer idNum = 0;
        if (crmContract1 !=null){
            Integer contractId = crmContract1.getContractId();
            idNum = contractId+1;

            String isNumStr = idNum+"";
            if(isNumStr.length() == 1){
                number = "00000"+isNumStr;
            }else if (isNumStr.length() == 2){
                number = "0000"+isNumStr;
            }else if (isNumStr.length() == 3){
                number = "000"+isNumStr;
            }else if (isNumStr.length() == 4){
                number = "00"+isNumStr;
            }else if (isNumStr.length() == 5){
                number = "0"+isNumStr;
            }else if (isNumStr.length() == 6){
                number = isNumStr;
            }
        }else {
            number = "000001";
        }
        //拼接字符串
        String str = "CTT"+strDate+number;

        //获取登录用户
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

        crmContract.setCreateTime(new Date());
        crmContract.setContractNo(str);
        crmContract.setCreator(users.getUserId());

        crmContractMapper.insertSelective(crmContract);
    }

    public void updateCrmContract(HttpServletRequest request, CrmContract crmContract) {
        crmContractMapper.updateByPrimaryKeySelective(crmContract);
    }

    public List<CrmContract> selectCrmContract(HttpServletRequest request, Map<String, Object> map,Integer page, Integer pageSize, Boolean useFlag) {
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        map.put("page", pageParams);
        if("".equals(request.getParameter("totalMin"))){
            map.put("totalMin",null);
        }
        if("".equals(request.getParameter("totalMax"))){
            map.put("totalMax",null);
        }
        List<CrmContract> list = crmContractMapper.selectCrmContract(map);
        ArrayList<CrmContract> crmContract = new ArrayList<>();
        for (CrmContract contract :list) {
            String creator = contract.getCreator();
            String username = usersMapper.getUsernameByUserId(creator);
            contract.setCreator(username);
            crmContract.add(contract);
        }
        return crmContract;
    }

    public CrmContract selectCrmContractById(HttpServletRequest request, Integer contractId) {
        CrmContract crmContract = crmContractMapper.selectCrmContractById(contractId);
        return crmContract;
    }

    public int updateDeleteStutus(HttpServletRequest request, CrmContract crmContract) {
        int a = crmContractMapper.updateDeleteStutus(crmContract);
        return a;
    }

    public int insertAll(List<CrmContract> list) {
        int flag = crmContractMapper.insertAll(list);
        return flag;
    }
}
